<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>

        <el-col :span="12">
          <el-form-item :label="$t('参加时间')" prop="workStartTime">
            <el-date-picker
              v-model="form.workStartTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('参加时间')"
              @change="checkDate"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('结束时间')" prop="workEndTime">
            <el-date-picker
              v-model="form.workEndTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('结束时间')"
              @change="checkDate"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('工作单位')" prop="corporateName">
            <el-input v-model="form.corporateName" :placeholder="$t('工作单位')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('职务')" prop="position">
            <el-input v-model="form.position" :placeholder="$t('职务')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('岗位')" prop="jobs">
            <el-input v-model="form.jobs" :placeholder="$t('岗位')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('证明人')" prop="witness">
            <el-input v-model="form.witness" :placeholder="$t('证明人')" show-word-limit maxlength="10" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('证明人联系方式')" prop="witnessContact">
            <el-input v-model="form.witnessContact" :placeholder="$t('证明人联系方式')" show-word-limit maxlength="20" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="hr:work" />
    </div>
  </div>
</template>

<script>
import { saveHrBaseWork, getHrBaseWork } from '@/api/oa/hr/hrBaseWork'

export default {
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        workStartTime: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        workEndTime: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        corporateName: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        position: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        jobs: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null && data.id !== undefined) {
        getHrBaseWork(data.id).then(response => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        hrId: undefined,
        workStartTime: undefined,
        workEndTime: undefined,
        corporateName: undefined,
        position: undefined,
        jobs: undefined,
        witness: undefined,
        witnessContact: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid && this.checkDate()) {
          this.loading = true
         this.$set(this.form, 'hrId', this.$route.query.hrId)
          saveHrBaseWork(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.checkDate()
        }
      })
    },
    checkDate() {
      if (this.form.workStartTime === null || this.form.workStartTime === undefined || this.form.workStartTime === '' ||
          this.form.workEndTime === null || this.form.workEndTime === undefined || this.form.workEndTime === '') {
        return true
      }
      const timeFlag = new Date(this.form.workStartTime).getTime() > new Date(this.form.workEndTime).getTime()
      if (timeFlag) {
        this.$message({
          message: '参加时间不能大于结束时间',
          type: 'warning'
        })
      }
      return !timeFlag
    }

  }

}
</script>
